<?php

namespace App\Http\Controllers\Home\album;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class AlbumController extends Controller
{
    // index()
    public function index()
    {
        $id = session()->get('master') -> id;
        $data = DB::table('album')
             -> where('uid',$id)
             -> select('uid','pic')
             -> get();
        // dd($data);

        return view('Home.album.index', ['data' => $data]);
    }

    // add() 添加相册
    public function add()
    {
        return view('Home.album.add');
    }

    // 处理提交过来的图片
    public function doadd(Request $request)
    {
        $data = $request -> except('_token');
        // dd($data);
        if($request -> file('pic') -> isValid())
        {
            $extension = $request -> file('pic') -> getClientOriginalExtension();
            // echo $extension; 上一步获取图片后缀  方便后面拼接
            $filename = mt_rand(100000000, 999999999).'.'.$extension;
            $dir = './uploads/album/';
            $request -> file('pic') -> move($dir, $filename);
            $userInfoData['pic'] = '/uploads/album/'.$filename;
        }
        // dd($userInfoData);
        //添加数据
        $id = session()->get('master') -> id;
        if($id)
        {
            $time = date('Y-m-d H:i:s');
            $userInfoData['uid']= $id;
            $userInfoData['created_at']= $time;
            $userInfoData['updated_at']= $time;
            // dd($userInfoData);
            $res = DB::table('album') -> insert($userInfoData);
            if($res)
            {
                return redirect('/Home/album/index') -> with(['info' => '添加成功']);
            }else{
                return back() -> with(['info' => '添加失败']);
            }
        }

    }
}
